/*
 * @Author: ls02 <2877455773@qq.com>
 * @Date: 2022-09-05 19:28:41
 * @LastEditors: ls02 <2877455773@qq.com>
 * @LastEditTime: 2022-09-06 12:05:59
 * @FilePath: \webapp\js\personal-center.js
 * @Description: 
 * 
 * Copyright (c) 2022 by ${ls02 <2877455773@qq.com>}, All Rights Reserved. 
 */
let app = new Vue({
    el:'.center',
    data:{
        name: "",
        user:{
            address: "",
            numb: "",
            password: "",
            flags: ""
        }
    },
    methods: {
        // 判定当前是否登录
        isLogin() {
            if (this.name == "") {
                return false;
            }
            return true;
        },
        logout() {
            this.name = "";
            window.location.href = "login.html";
        },
        setPassword() {
            console.log("setPassword");
            this.user.flags = 1;
            $.ajax({
                type: "POST",
                url: "setUser?name=" + app.name,
                data: JSON.stringify(app.user),
                success:function(data, status) {
                    // if (status.ok != 1) {
                    //     console.log("修改失败！" + status.ok);
                    //     alert("修改密码失败！");
                        
                    //     return;
                    // }
                    alert("修改密码成功！");
                }
            });
        },
        setAddress() {
            console.log("setAddress");
            this.user.flags = 2;
            console.log(this.flags);
            
            $.ajax({
                type: "POST",
                url: "setUser?name=" + app.name,
                data: JSON.stringify(app.user),
                success:function(data, status) {
                    // if (status.ok != 1) {
                    //     console.log("修改地址！");
                    //     alert("修改密码地址！");
                        
                    //     return;
                    // }
                    alert("修改地址成功！");
                }
            });
        },
        setNumb() {
            console.log("setNumb");
            this.user.flags = 3;
            $.ajax({
                type: "POST",
                url: "setUser?name=" + app.name,
                data: JSON.stringify(app.user),
                success:function(data, status) {
                    // if (status.ok != 1) {
                    //     console.log("修改手机号失败！");
                    //     alert("修改手机号失败！");
                        
                    //     return;
                    // }
                    alert("修改手机号成功！");
                }
            });
        },
            // 判定当前是否登录
            isLogin() {
                if (this.name == "") {
                    return false;
                }
                return true;
            },
            // 从服务器检测登录状态
            checkLogin() {
                // 页面最初加载时先访问服务器判定自身的登陆状态. 
                console.log("checkLogin");
                $.ajax({
                    url: 'login',
                    type: 'get',
                    success: function (data, status) {
                        if (data.ok != 1) {
                            return;
                        }
                        // 登录成功后
                        // 1. 设置用户名
                        app.name = data.name;
                    }
                });
            }
    }
});